package br.com.droid;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import br.com.droid.model.Email;

public class BancoEmail {

	private static BancoEmail instance;
	private ArrayList<Email> listaEmails;
	
	public BancoEmail() {
		listaEmails = new ArrayList<Email>();
	}
	
	public static BancoEmail getBancoInstance(){
		if(instance==null)
			instance = new BancoEmail();
		return instance;
	}
		
	public Email buscarEmail(int id){
		Email email = null;
		String teste = "SELECT * from Email where id = +"+id+";";
        
        if(ConexaoMySQL.getConexaoMySQL()!=null){
            Statement stm;
			try {
				stm = ConexaoMySQL.getConexaoMySQL().createStatement();
				ResultSet rs = stm.executeQuery(teste);
		            while (rs.next()) {
		                email = new Email();
		                email.setId(rs.getLong("id"));
		                email.setId_centro(rs.getLong("id_centro"));
		        		email.setEmail(rs.getString("email"));		        		
		            }
	            ConexaoMySQL.FecharConexao();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
		return email;
	}
	
	public ArrayList<Email> getListaEmails(){
        String teste = "SELECT * from Email;";
        listaEmails.clear();
        
        if(ConexaoMySQL.getConexaoMySQL()!=null){
            Statement stm;
			try {
				stm = ConexaoMySQL.getConexaoMySQL().createStatement();
				ResultSet rs = stm.executeQuery(teste);
		            while (rs.next()) {
		            	Email email = new Email();
		                email.setId(rs.getLong("id"));
		                email.setId_centro(rs.getLong("id_centro"));
		        		email.setEmail(rs.getString("email"));
		        		listaEmails.add(email);
		            }
	            ConexaoMySQL.FecharConexao();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
		return listaEmails;
	}
}
